package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SignInMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @Author whx
 * @Date 2025/1/11 14:21
 */
@Component
@Slf4j
public class LearningPointsListener {

    @Autowired
    private IPointsRecordService pointsRecordService;

    // 监听写回答
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "reply.new.queue"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY
    ))
    public void replyNewPoints(Long userId){
        pointsRecordService.addPointsRecord(userId, 5, PointsRecordType.QA);
    }

    // 监听签到
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "sign.in.points.queue"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN
    ))
    public void signInPoints(SignInMessage message){
        pointsRecordService.addPointsRecord(message.getUserId(), message.getPoints(), PointsRecordType.SIGN);
    }

    // 监听学习
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "section.learned.queue"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.LEARN_SECTION
    ))
    public void learnedPoints(Long userId){
        pointsRecordService.addPointsRecord(userId, 10, PointsRecordType.LEARNING);
    }



}
